import jsonp from '../common/js/jsonp';
import {commonParams, options} from './config';
import {Singer} from '../common/js/singer.js';

export function getSingerList () {
  const url = `https://c.y.qq.com/v8/fcg-bin/v8.fcg`;
  const data = Object.assign({}, commonParams, {
    channel: 'singer',
    page: 'list',
    key: 'all_all_all',
    pagesize: 100,
    pagenum: 1,
    g_tk: 5381,
    loginUin: 0,
    hostUin: 0,
    platform: 'yqq',
    needNewCode: 0
  });
  return jsonp(url, data, options);
}

const HotCount = 10;

export function normaliseList (list) {
  // console.log(list);
  let map = {
    hot: {
      title: '热门',
      item: []
    }
  };
  list.map((item, i) => {
    let singer = new Singer(item.Fsinger_mid, item.Fsinger_name);
    if (i < HotCount) {
      map.hot.item.push(singer);
    }
    const key = item['Findex'];
    if (!map[key]) {
      map[key] = {
        title: key,
        item: []
      };
    }
    map[key].item.push(singer);
  });
  // 排序map
  let ret = [];
  let hot = [];
  for (let key in map) {
    let v = map[key];
    if (v.title.match(/[a-zA-Z]/)) {
      ret.push(v);
    } else {
      hot.push(v);
    }
  }
  ret.sort((a, b) => {
    return a.title.charCodeAt(0) - b.title.charCodeAt(0);
  });
  return hot.concat(ret);
}

export function getSingerDetail (singerId) {
  const url = 'https://c.y.qq.com/v8/fcg-bin/fcg_v8_singer_track_cp.fcg';
  const paras = Object.assign({}, commonParams, {
    singerid: singerId,
    needNewCode: 0,
    order: 'listen',
    num: 80,
    uin: 0,
    platform: 'h5page'
  });
  return jsonp(url, paras, options);
}
